<?php
// 微信端 工作总结
namespace H5\Controller;
use Think\Controller;
class WorkController extends CommonController {
    // 列表页
    public function index(){
        $userid = session('userid');
        $level  = session('level');
        if($level == 1) {  // 普通销售员身份
            $map['uid'] = $userid;
        }
    	$list = M('work')->where($map)->order('date desc,id desc')->select();
    	$workList = get_works_list($list,true);
        $this->assign('workList',$workList);
        $this->assign('menu','work'); // 工作总结
        $this->display();
    }

    // 详情页
    public function detail(){
    	$id = I('get.id');
        $map['id'] = $id;
    	$arr = M('work')->where($map)->select();
    	$workArr = get_works_list($arr,true);
        $date = date('Y-m-d');
        $this->assign('date',$date);  // 用于判断是否是今天的工作总结，是则可修改
        $this->assign('workArr',$workArr[0]);
        $this->assign('menu','work'); // 工作总结
        $this->display();
    }

    // 添加页
    public function add(){
        $this->assign('menu','work'); // 工作总结
    	$this->display();
    }

    // 添加提交
    public function addCheck(){
    	$map['uid'] = session('userid');
    	$date = I('post.date');
        $map['date'] = strtotime($date); // 总结日期
    	$arr = M('work')->where($map)->order('id desc')->find();
    	if(!$arr){ // 判断当前日期总结是否添加过
    		$map['time']    = time(); // 添加时间
            $map['content'] = I('post.content');
    		$rel = M('work')->add($map); // 保存总结记录
	    	if($rel){ // 保存成功,则继续保存总结内容
	    		 $msg = array('state'=>200, 'content'=>'添加成功');
	    	}else{
	    		 $msg = array('state'=>500, 'content'=>'添加失败');
	    	}
    	}else{
    		$msg = array('state'=>500, 'content'=>'当前日期工作总结已添加');
    	}
    	$this->ajaxReturn($msg);
    }

    // 修改页
    public function edit(){
        $id = I('get.id');
        $map['id'] = $id;
        $arr = M('work')->where($map)->select();
        $workArr = get_works_list($arr,false);
        $this->assign('workArr',$workArr[0]);
        $this->assign('menu','work'); // 工作总结
        // p($workArr);die;
        $this->display();
    }

    // 修改提交  
    public function editCheck(){
        $data['id']      = I('get.id');
        $data['content'] = I('post.content');
        $rel = M('work')->save($data);
        if($rel){
            $this->ajaxReturn(array('state'=>200, 'content'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('state'=>500, 'content'=>'修改失败'));
        }
    }



}
